package com.axxc.jpa.specification;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;

import com.axxc.jpa.entity.EnglishWordNoteBookEntity;
import com.axxc.jpa.out.filter.EnglishWordNoteBookFilter;

/**
 * @ClassName: EnglishWordNoteBookSpec
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author xxc
 * @date 2018年8月20日 下午2:46:59
 * @see {@link https://gitee.com/biuabiu}
 */
public class EnglishWordNoteBookSpec implements Specification<EnglishWordNoteBookEntity> {

	private EnglishWordNoteBookFilter filter;

	public EnglishWordNoteBookSpec(EnglishWordNoteBookFilter filter) {
		super();
		this.filter = filter;
	}

	@Override
	public Predicate toPredicate(Root<EnglishWordNoteBookEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
		// TODO 筛选条件
		Predicate predicate = cb.conjunction();//1=1
		if (filter != null) {
			if (null != filter.getId() && filter.getId().longValue() > 1) {
				predicate = cb.and(predicate,cb.equal(root.get("id"),filter.getId()));
			}
			if (null != filter.getDeleteStatus()) {
				predicate = cb.and(predicate,cb.equal(root.get("deleteStatus"),filter.getDeleteStatus()));
			}
			if (StringUtils.isNotBlank(filter.getKeyword())) {
				Predicate predicate2 = cb.like(root.get("englishWord"), "%" + filter.getKeyword() + "%");
				predicate2 = cb.or(predicate2, cb.like(root.get("chineseMeaning"), "%" + filter.getKeyword() + "%"));
				predicate2 = cb.or(predicate2, cb.like(root.get("chineseMeaningLongSpell"), "%" + filter.getKeyword() + "%"));
				predicate2 = cb.or(predicate2, cb.like(root.get("chineseMeaningShortSpell"), "%" + filter.getKeyword() + "%"));
				predicate = cb.and(predicate,predicate2);
			}
		}
		return predicate;
	}

}
